﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using System.Drawing;
using System.Timers;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_RingOfFire : AreaSpell, IRootBuff
    {
        public Spell_RingOfFire() { }

        public string RootMessage()
        {
            return "Your path is blocked by a raging inferno";
        }

        public override bool isDangerous(Creature creature)
        {
            return false;
        }

        private int removeImageCounter = 0;

        public void Cast(CreatureBase myCreatureBase, int castLevel, List<string> MoveList)
        {
            spellTimer.Interval = 1250;

            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, castLevel, MoveList, out MapItemsWithin12) == false)
                return;

            //Set Duration & Damage Per Character Stats
            DurationLow = 1;
            DurationHigh = castLevel;

            List<MapItem> MapItemsWithin8 = new List<MapItem>();
            List<Character> CharactersWithin8 = new List<Character>();
            List<MapItem> MapItemsToCast = new List<MapItem>();
            foreach (MapItem mapitem in MapItemsWithin12)
            {
                if (Math.Abs(mapitem.MapX - CastMapX) <= 8 && Math.Abs(mapitem.MapY - CastMapY) <= 8)
                {
                    MapItemsWithin8.Add(mapitem);
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                            CharactersWithin8.Add((Character)creaturebase);
                    }
                }
                if (mapitem.MapX == CastMapX && mapitem.MapY == CastMapY)
                {
                    MapItemsToCast.Add(mapitem);
                }
            }

            if (MapItemsToCast.Count == 0 || MapItemsToCast[0].MotionCreatureOverride != -1 || MapItemsToCast[0].ContainsSpellByStackType(spellIcon.StackType))
            {
                foreach (Character character in CharactersWithin8)
                {
                    character.PlaySound("RingOfFire", CastMapX, CastMapY);
                }
                return;
            }
 
            foreach (MapItem mapitem in MapItemsToCast)
            {
                mapitem.SpellLayer0.Add(this);
                mapitem.MotionCreatureOverride = 0;
            }

            base.MapItems = MapItemsToCast;

            foreach (Character character in CharactersWithin8)
            {
                character.AddMapItems(MapItemsToCast,false);
                character.PlaySound("RingOfFire", CastMapX, CastMapY);
            }

            //Start Spell Timer
            spellTimer.Start();
        }
        
        protected override void SpellTimer_Expired(object source, ElapsedEventArgs e)
        {
            TimerCounter = TimerCounter + 1;
            
            //Get MapLock
            LockItem MapLock = Managers.GameHandler.GetLockItem(MapID, Instance);
            
            //Lock MapLock
            lock (MapLock.LockObj)
            {
                //Stop Running if the image is gone.
                if (MapItems[0].ContainsSpellByID(SpellID) == false)
                    return;

                //Try to release creatures
                if (removeImageCounter == 0)
                {
                    if (TimerCounter >= DurationLow)
                    {
                        int RemoveSpell = Managers.GameHandler.random.Next(-1, DurationHigh - TimerCounter);
                        if (RemoveSpell == -1 || TimerCounter >= DurationHigh)
                        {
                            MapItems[0].MotionCreatureOverride = -1;//getMotion(mapitem.MapX, mapitem.MapY);
                            removeImageCounter++;
                        }
                    }
                }
                //Creatures released, wait a few to remove spell image.
                else
                {
                    removeImageCounter++;

                    //Wait duration complete. Do not restart spell timer.
                    if (removeImageCounter >= 5)
                    {
                        //If image still exists, remove it and update the nearby characters.
                        if (MapItems[0].ContainsSpellByID(SpellID))
                        {
                            MapItems[0].RemoveSpellByID(SpellID);

                            foreach (MapItem mapitem in MapItems[0].mapItemsWithin4)
                            {
                                foreach (CreatureBase creatureBase in mapitem.CreaturesBases)
                                {
                                    if (creatureBase is Character)
                                    {
                                        Character character = (Character)creatureBase;
                                        character.AddMapItem(MapItems[0]);
                                    }
                                }
                            }
                        }

                        return;
                    }
                }
            }
            spellTimer.Start();
        }

    }

    
}
